<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('行政区域管理列表')" />
</head>
<body class="gray-bg">
     <div class="container-div">
        <div class="row">
            <div class="col-sm-12 search-collapse">
                <form id="formId">
                    <div class="select-list">
                        <ul>
                            <!--<li>
                                <label>区域编码：</label>
                                <input type="text" name="areaId"/>
                            </li>-->
                            <li>
                                <label>区域名称：</label>
                                <input type="text" name="areaName"/>
                            </li>
                            <!--<li>
                                <label>区域级别：</label>
                                <input type="text" name="areaRank"/>
                            </li>
                            <li>
                                <label>上一级ID：</label>
                                <input type="text" name="fatherId"/>
                            </li>
                            <li>
                                <label>创建时间：</label>
                                <input type="text" class="time-input" placeholder="请选择创建时间" name="createTime"/>
                            </li>
                            <li>
                                <label>更新时间：</label>
                                <input type="text" class="time-input" placeholder="请选择更新时间" name="updateTime"/>
                            </li>-->
                            <li>
                                <a class="btn btn-primary btn-rounded btn-sm" onclick="$.treeTable.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                            </li>
                        </ul>
                    </div>
                </form>
            </div>

            <div class="btn-group-sm" id="toolbar" role="group">
                <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="system:area:add">
                    <i class="fa fa-plus"></i> 新增
                </a>
                <a class="btn btn-primary" onclick="$.operate.edit()" shiro:hasPermission="system:area:edit">
                    <i class="fa fa-edit"></i> 修改
                </a>
                <a class="btn btn-info" id="expandAllBtn">
                    <i class="fa fa-exchange"></i> 展开/折叠
                </a>
            </div>
            <div class="col-sm-12 select-table table-striped">
                <table id="bootstrap-tree-table"></table>
            </div>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <script th:inline="javascript">
        var addFlag = [[${@permission.hasPermi('system:area:add')}]];
        var editFlag = [[${@permission.hasPermi('system:area:edit')}]];
        var removeFlag = [[${@permission.hasPermi('system:area:remove')}]];
        var prefix = ctx + "system/area";
        let areaNames = new Array();
        let areaNamesIndex = 0;

        $(function() {
            var options = {
                code: "areaId",
                parentCode: "fatherId",
                expandColumn: "2",
                uniqueId: "id",
                url: prefix + "/list",
                createUrl: prefix + "/add/{id}",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
                exportUrl: prefix + "/export",
                modalName: "行政区域管理",
                columns: [{
                    field: 'selectItem',
                    radio: true
                },
                {
                    field: 'areaId',
                    title: '区域编码',
                    align: 'left'
                },
                {
                    field: 'areaName',
                    title: '区域名称',
                    align: 'left'
                },
                {
                    field: 'areaRank',
                    title: '区域级别',
                    align: 'left'
                },
                {
                    field: 'fatherId',
                    title: '上一级ID',
                    align: 'left'
                },
                {
                    field: 'createTime',
                    title: '创建时间',
                    align: 'left'
                },
                {
                    field: 'updateTime',
                    title: '更新时间',
                    align: 'left'
                },
                {
                    title: '操作',
                    align: 'center',
                    align: 'left',
                    formatter: function(value, row, index) {
                        areaNames[areaNamesIndex++] = row.areaName;
                        var actions = [];
                        actions.push('<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.id + '\')"><i class="fa fa-edit"></i>编辑</a> ');
                        actions.push('<a class="btn btn-info  btn-xs ' + addFlag + '" href="javascript:void(0)" onclick="$.operate.add(\'' + row.id + '\')"><i class="fa fa-plus"></i>新增</a> ');
                        actions.push('<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="del(\'' + row.areaId + '\')"><i class="fa fa-remove"></i>删除</a>');
                        return actions.join('');
                    }
                }]
            };
            $.treeTable.init(options);
        });

        function del(areaId) {
            // $.operate.remove(rowId);
            table.set();
            $.modal.confirm("确定删除该条" + table.options.modalName + "信息吗？", function() {
                //var url = $.common.isEmpty(rowId) ? table.options.removeUrl : table.options.removeUrl.replace("{id}", rowId);
                var data = { "fatherId": parseInt(areaId) };
                $.ajax ({
                    url: table.options.removeUrl,
                    type: 'post',
                    dataType: 'json',
                    data: data,
                    success: function(data) {
                        if (data.res == 'success') {
                            $.modal.msgSuccess("删除成功！");
                            $.treeTable.refresh();
                        } else if (data.res == 'fail') {
                            $.modal.msgError("该行政区域含有子级区域，拒绝删除！");
                        } else {
                            $.modal.msgError("删除失败！请联系管理员！");
                        }
                    }
                });
            });

        }

    </script>
</body>
</html>
